package kz.sdu.javaee.techsupport.gui;

/**
 * Created by IntelliJ IDEA.
 * User: true
 * Date: 4/26/12
 * Time: 11:01 PM
 * To change this template use File | Settings | File Templates.
 */

import com.vaadin.event.ShortcutAction;
import com.vaadin.ui.*;
import kz.sdu.javaee.techsupport.TeApplication;
import kz.sdu.javaee.techsupport.ws.UserDTO;
import kz.sdu.javaee.techsupport.ws.UserServices;
import kz.sdu.javaee.techsupport.ws.UserServicesWS;

import javax.xml.ws.BindingProvider;

/**
 * Created by IntelliJ IDEA.
 * User: pavel
 * Date: 02.09.11
 * Time: 15:05
 * To change this template use File | Settings | File Templates.
 */

public final class LoginWindow extends Window {
    private TeApplication application;
    private VerticalLayout layoutForLogin;
    private VerticalLayout loginPanelVLayout;
    private GridLayout layout;

    private TextField usernameField;
    private PasswordField passwordField;
    private Button loginButton;

    public LoginWindow(final TeApplication application) {
        this.application = application;
        setName("auth");

        layout = new GridLayout(1,3);
        layout.setSizeFull();
        layout.setSpacing(true);

        this.addComponent(layout);

        loginPanelVLayout = new VerticalLayout();
        loginPanelVLayout.setSpacing(true);
        loginPanelVLayout.setMargin(true);
        loginPanelVLayout.setWidth("400");
        loginPanelVLayout.setStyleName("ps-login-box");

        Label systemNameLabel = new Label("<b>Вход в систему</b>");
        systemNameLabel.setContentMode(Label.CONTENT_XHTML);
        systemNameLabel.setStyleName("ps-login-system-name");

        loginPanelVLayout.addComponent(systemNameLabel);

        usernameField = new TextField("Пользователь", "");
        usernameField.setWidth("350px");
        usernameField.setTabIndex(1);
        passwordField = new PasswordField("Пароль", "");
        passwordField.setWidth("350px");
        passwordField.setTabIndex(2);
        loginButton = new Button("Вход");
        loginButton.setTabIndex(3);
        loginButton.setImmediate(true);
        loginButton.setClickShortcut(ShortcutAction.KeyCode.ENTER);
        loginButton.addListener(new Button.ClickListener() {
            public void buttonClick (Button.ClickEvent event) {
                UserServices ws = new UserServices();
                UserServicesWS port = ws.getUserServicesWSPort();
                ((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, (String) usernameField.getValue());
                ((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, (String) passwordField.getValue());
                UserDTO user = port.authorize((String) usernameField.getValue(), (String) passwordField.getValue());
                getWindow().showNotification(user.getUsername()+":"+user.getPassword()+":"+user.getUserType().getUserType());
                application.showMainWindow(user);
            }
        });

        loginPanelVLayout.addComponent(usernameField);
        loginPanelVLayout.addComponent(passwordField);
        loginPanelVLayout.addComponent(loginButton);
        loginPanelVLayout.setComponentAlignment(loginButton, Alignment.MIDDLE_CENTER);

        layoutForLogin = new VerticalLayout();
        layoutForLogin.setHeight("600");
        layoutForLogin.addComponent(loginPanelVLayout);
        layoutForLogin.setComponentAlignment(loginPanelVLayout,Alignment.MIDDLE_CENTER);

        layout.addComponent(layoutForLogin,0,1);
    }

}